//
//  Ctrl.cpp
//  app-ios gl
//
//  Created by MeherTj on 2022/1/21.
//  Copyright © 2022 lover. All rights reserved.
//

#include "Ctrl.hpp"


Ctrl * Ctrl::Get()
{
	static Ctrl ctrl;
	return &ctrl;
}

void Ctrl::startGame()
{
	m_tetris.reset();

	getCurrentViewPlay()->setGestureEvent(std::bind(&Ctrl::onGesture, this, std::placeholders::_1));

	getCurrentViewPlay()->setCtrlUpdate([this](float delay) {
		this->onUpdate(delay);
	});

	getCurrentViewPlay()->updateMap(m_tetris.getMap());

	this->nextTurn();
}

void Ctrl::nextTurn() {
	m_tetris.gen_tetris();
	m_tetris.reset_current_pos();

	getCurrentViewPlay()->updateNextTetris(m_tetris.getNextTetris());
	getCurrentViewPlay()->updateCurrentTetris(m_tetris.getCurrentTetris(), m_tetris.getCurrentTetrisPos());
	m_is_running = true;
}

void Ctrl::onGesture(GestureType type) {
	if(type == GestureType::End)
	{
		m_is_touched = false;
		return;
	}
	if(!m_is_running)
	{
		LogW("Touch Not Running@Move not touched");
		return;
	}

	if(type == GestureType::Begin)
	{
		m_is_touched = true;
		return;
	}
	if(!m_is_touched) return;

	this->moveTetris(type);
}

void Ctrl::syncTetris()
{
	getCurrentViewPlay()->updateCurrentTetris(m_tetris.getCurrentTetris(), m_tetris.getCurrentTetrisPos());
}

void Ctrl::moveTetris(GestureType m) {

	if(m == GestureType::QuickDown) {
		m_tetris.try_quick_down();
		this->putTetris();
		return;
	}

	bool res = false;
	switch (m) {
		case GestureType::Left:
			AudioPlayer::Get()->play(GetPath("sound move"), 0);
			res = m_tetris.try_left();
			break;
		case GestureType::Right:
			AudioPlayer::Get()->play(GetPath("sound move"), 0);
			res = m_tetris.try_right();
			break;
		case GestureType::Down:
			AudioPlayer::Get()->play(GetPath("sound move", 0), 0);
			res = m_tetris.try_down();
			break;
		case GestureType::Tap:
			AudioPlayer::Get()->play(GetPath("sound rotate"), 0);
			res = m_tetris.try_rotate();
			break;

		default:
			break;
	}
	if(res) this->syncTetris();
}

void Ctrl::putTetris()
{
	m_is_touched = false;
	m_is_running = false;
	
	AudioPlayer::Get()->play(GetPath("sound tick"), 0);

	if(!m_tetris.put_in()) {
		getCurrentViewPlay()->updateCurrentTetris(m_tetris.getCurrentTetris(), m_tetris.getCurrentTetrisPos());
		this->finishGame();
		AudioPlayer::Get()->play(GetPath("sound failed"), 0);
	}
	else {
		this->checkResult();
	}
}
void Ctrl::checkResult()
{
	getCurrentViewPlay()->updateMap(m_tetris.getMap());
	m_tetris.check_result();

	if(m_tetris.getResult().cleared_lines.size())
	{
		getCurrentViewPlay()->checkResult(m_tetris.getResult(), [this]() {
			m_tetris.checkout();
			getCurrentViewPlay()->updateMap(m_tetris.getMap());
			getCurrentViewPlay()->updateScore(m_tetris.getScore());
			this->nextTurn();
		});
		AudioPlayer::Get()->play(GetPath("sound cleared"), 0);
	}
	else {
		this->nextTurn();
	}}
void Ctrl::finishGame()
{
	getCurrentViewPlay()->runFaild([this]() {
//		Activity::Get()->getRootNode()->removeAllNodes();
	});
	LogD("finish game");
}
void Ctrl::onUpdate(float interval) {
	if(!m_is_running) return;
	m_update_time += interval;

//	return;
	
	if(m_down_time + m_down_interval < m_update_time) {
		m_down_time = m_update_time;
		if(m_tetris.try_down()) {
			this->syncTetris();
		}
		else {
			this->putTetris();
		}
	}
}
